package com.power.sifa.approve.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import com.power.sifa.approve.model.Apl_target;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 被申请人 持久层(hibernate)
 */

public interface Apl_targetDao extends JpaRepository<Apl_target, Serializable>, JpaSpecificationExecutor<Apl_target>, Repository<Apl_target, Serializable>, Apl_targetDaoCustom<Apl_target, Serializable> {

   @Modifying
   @Transactional
   @Query("delete from Apl_target s where  s.id in (:ids) ")
   void removeByIds(@Param("ids") List<Integer> ids);

   /**
    * 更加applyid删除记录
    * @param applyid
    */
   @Modifying
   @Transactional
   @Query("delete from Apl_target s where  s.applyid = :applyid")
   void removeByApplyid(@Param("applyid") String applyid);

   /**
    * 首页统计-被申请人top10统计
    * @param applyids
    */
   @Query(value = " select name, count(name) as count from Apl_target where applyid in (:applyids) group by name order by count(name) desc LIMIT 0,10 ", nativeQuery = true)
   List<Map<String, Object>> countTargetTop10(@Param("applyids") List<String> applyids);

   /**
    * 通过applyid 和 phones 查询被申请人的数据
    * @param applyid
    * @param phones
    * @return
    */
   @Query(value = " select * from Apl_target where applyid = :applyid and phone in (:phones) ", nativeQuery = true)
   List<Apl_target> getTargetListByApplyidAndPhone(@Param("applyid") String applyid, @Param("phones") List<String> phones);

}
